//
//  MainController.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/19.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class MainController: UITabBarController,SHTabBarDelegate
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        //利用KVC的方法来设置UITabBarController上的TabBar
        let tabBar = MainTabBar()
        
        tabBar.AddDelegate = self
        
        
        setValue(tabBar, forKey: "tabBar")
        
        
        
        //加载4个navigation Controller
        self.setUpNavigationController()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }
    
    //加载4个navigation Controller
    func setUpNavigationController()
    {
        let homeNav = setUpController("首页", imageStr:"tabbar_home", vc: HomeController())
        
        let MessageNav = setUpController("消息", imageStr: "tabbar_message_center", vc: MessageController())
        
        let DiscoveryNav = setUpController("发现", imageStr:"tabbar_discover", vc: DiscoveryController())
        
        let ProfileNav = setUpController("我", imageStr: "tabbar_profile", vc: ProfileController())
        
        self.viewControllers = [homeNav,MessageNav,DiscoveryNav,ProfileNav]
        
    }
    
    //利用title和image和VC来创建navigationController
    func setUpController(title:String,imageStr:String,vc:UIViewController) -> UINavigationController
    {
        
        //设置tabBarItem的title
        /*
        vc.tabBarItem.title = title
        
        //设置navigationItem的title
        vc.navigationItem.title = title
        */
        //或直接设置title可以达到上面两句代码的功效
        vc.title = title
        
        
        
        //设置tabBarItem的image
        //保持图片的最原始状态
        vc.tabBarItem.image = UIImage(named:imageStr)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置tabBarItem点击之后的image
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageStr)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置点击之后的文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //返回UINavigationController
        let nav:UINavigationController = UINavigationController(rootViewController: vc)
        
        return nav
    }
    
    //实现协议的方法
    func addButtonClick()
    {
        
        let addSomeView:AddSomeView = AddSomeView()
        
        //将AddSomeView添加到此时的view上
        self.view.addSubview(addSomeView)
        
        //设置addSomeView的约束
        addSomeView.snp_makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        //设置addSomeView的targetVC
        addSomeView.target = self
    
        
        
        //添加addSomeView之后开启按钮的动画
        addSomeView.buttonShow()
        
        //设置UINavigationController(AddWordController()是其rootViewController）
        //let naVC = UINavigationController(rootViewController: AddWordController())
        //self.presentViewController(naVC, animated: true, completion: nil)
        
    }

}
